package module.scene.view.group
{
	import com.darwin.darwinIsometric.core.IsoObject;
	import com.darwin.darwinIsometric.core.IsoPosition;
	import com.darwin.darwinIsometric.core.Point3D;
	import com.darwin.darwinIsometric.utils.IsoUtils;
	
	import flash.geom.Point;
	
	import module.scene.consts.SceneConst;

	/**
	 * Iso群组控制类 
	 * @author sam
	 * 
	 */	
	public class BaseIsoGroup
	{
		
		private var _isoAry:Array;
		private var _floorWidth:Number;
		private var _floorHeight:Number;
		private var _x3d:int;
		private var _y3d:int;
		private var _z3d:int;
		public var x:int;
		public var y:int;
		private var _offsetX:int;
		private var _offsetY:int;
		private var _isoPositionChanged:Boolean;
		private var _isoPosition:IsoPosition;
		private var _middleX:Number = 0;
		private var _middleZ:Number = 0;
		private var dValueX:int;	//群组IsoPosition  X偏移量
		private var dValueZ:int;
		private var _direction:int = 0;
		private var _size:int = SceneConst.GRID_SIZE;
		public function BaseIsoGroup()
		{
			
		}
		
		/**
		 * 设置ISO群组 数据  
		 * @param dataAry 数据类型必须是IsoObject或其子类
		 * 
		 */		
		public function set isoGroup(dataAry:Array):void{
			_isoAry = dataAry;
		}
		
		public function get isoGroup():Array{
			return _isoAry;
		}

		/**
		 * 物件底部的高，floorHeight表示在z方向底部所占用的宽度
		 * @return 
		 */
		public function get floorHeight():Number
		{
			return _floorHeight;
		}
		
		public function set floorHeight(value:Number):void
		{
			_floorHeight = value;
			_isoPositionChanged = true;
		}
		
		/**
		 * 物件底部的宽，floorWidth表示在x方向底部所占用的宽度
		 * @return 
		 */
		public function get floorWidth():Number
		{
			return _floorWidth;
		}
		
		public function set floorWidth(value:Number):void
		{
			_floorWidth = value;
			_isoPositionChanged = true;
		}

		/**
		 * 物体的方向,可选值为 45 90 135 180 225 270 315,默认为0面对Z轴方向,旋转为顺时针
		 */
		public function set direction(value:int):void
		{
			_direction = value;
			changeDirection();
		}
		public function get direction():int
		{
			return _direction;
		}
		
		/**
		 * 2.5D x坐标，x增大物件朝屏幕右下角移动
		 * @return 
		 */
		public function get x3d():Number
		{
			return _x3d;
		}
		
		public function set x3d(value:Number):void
		{
			_x3d = value;
			createXY();
		}
		
		public function get y3d():Number
		{
			return _y3d;
		}
		
		public function set y3d(value:Number):void
		{
			_y3d = value;
			createXY();
		}
		
		/**
		 * 2.5D z坐标，z增大物件朝屏幕
		 * @return 
		 */
		public function get z3d():Number
		{
			return _z3d;
		}
		
		public function set z3d(value:Number):void
		{
			_z3d = value;
			createXY();
		}
		
		/**
		 * 2D图形相对于2.5D原点的偏移量
		 */
		public function get offsetX():Number
		{
			return _offsetX;
		}
		
		public function set offsetX(value:Number):void
		{
			_offsetX = value;
			createXY();
		}
		
		/**
		 * 2D图形相对于2.5D原点的偏移量
		 */
		public function get offsetY():Number
		{
			return _offsetY;
		}
		
		public function set offsetY(value:Number):void
		{
			_offsetY = value;
			createXY();
		}

		/**
		 * 获得占地中心点的x坐标，如果对象是长条形取得的坐标位置可能会不准确
		 * 
		 */
		public function get middleX():Number
		{
			countPosition();
			return _middleX;
		}

		/**
		 * 获得占地中心点的y坐标，如果对象是长条形取得的坐标位置可能会不准确
		 */
		public function get middleZ():Number
		{
			countPosition();
			return _middleZ;
		}
		
		/**
		 * 2.5D 物件坐标 
		 */
		public function get postion3D():Point3D
		{
			return new Point3D(x3d,y3d,z3d);
		}
		
		/**
		 * @private
		 */
		public function set postion3D(value:Point3D):void
		{
			_x3d = value.x;
			_y3d = value.y;
			_z3d = value.z;
			createXY();
		}

		
		/**
		 * 获得等角对象所占用格子中最靠近原点的isoPosition值，isoPosition是基于网格计算的坐标值
		 * @return 
		 */
		public function get isoPosition():IsoPosition
		{
			countPosition();
			return _isoPosition;
		}
		
		/**
		 * 根据 x3d,y3d,z3d,offsetX,offsetY计算得出IsoObject的2D坐标，
		 * 2D坐标用于渲染
		 */
		protected function createXY():void
		{
			var $point:Point = IsoUtils.isoToScreen(new Point3D(x3d,y3d,z3d));
			this.x = $point.x + offsetX;
			this.y = $point.y + offsetY;
			_isoPositionChanged = true;
			countPosition();
		}
		
		protected function countPosition():void
		{
			if(_isoPositionChanged){
				var $isox:int = Math.floor(this.x3d / _size);
				var $isoz:int = Math.floor(this.z3d / _size);
				
				var $isomx:int = Math.floor((this.x3d + this.floorWidth-1) / _size);
				var $isomz:int = Math.floor((this.z3d + this.floorHeight-1) / _size);
				
				_middleX = this.floorWidth/2 + this.x3d;
				_middleZ = this.floorHeight/2 + this.z3d;
				
				_isoPosition = new IsoPosition($isox,$isoz);
				if(_isoAry.length>0){
					dValueX = (_isoAry[0] as IsoObject).isoPosition.x- _isoPosition.x;
					dValueZ= (_isoAry[0] as IsoObject).isoPosition.z- _isoPosition.z;
				}
				refreshGroupPositon();
				_isoPositionChanged = false;
			}
		}
		
		/**
		 * 清除群组里的一个Iso 
		 * @param iso
		 * 
		 */		
		public function deleteIso(iso:IsoObject):void{
			for(var i:int = 0;i<_isoAry.length;i++){
				var tmpIso:IsoObject = _isoAry[i] as IsoObject;
				if(tmpIso == iso){
					_isoAry = _isoAry.splice(i,1);
					break;
				}
			}
		} 
		
		/**
		 * 增加一个新的Iso到群组 
		 * @param iso
		 * 
		 */		
		public function addNewIso(iso:IsoObject):void{
			if(_isoAry.length>0){
				_isoAry.push(iso);
			}else{
				_isoAry = new Array();
				_isoAry.push(iso);
			}
		}
		
		/**
		 * 销毁 
		 * 
		 */		
		public function despose():void{
			_isoAry = null;
		}
		
		/**
		 *  改变群组朝向 
		 * 
		 */		
		protected function changeDirection():void{
			for each(var iso:IsoObject in _isoAry){
				iso.direction = _direction;
			}
		}
		
		/**
		 * 根据群组第一个对象的isoPostion3d，改变群组里其他对象的isoPostion3D 
		 * 
		 */		
		protected function refreshGroupPositon():void{
			if(_isoAry.length>0){
				for(var i:int=0;i<_isoAry.length;i++){
					var tmpIso:IsoObject = _isoAry[i] as IsoObject;
					if(i==0){
						tmpIso.isoPosition.x = _isoPosition.x;
						tmpIso.isoPosition.z = _isoPosition.z;
					}else{
						tmpIso.isoPosition.x += dValueX;
						tmpIso.isoPosition.z += dValueZ;
					}
					tmpIso = null;
				}
			}
		}
	}
}